//
//  Player_TagView.m
//  SoundPill
//
//  Created by 宋旭东 on 15/4/15.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "Player_TagView.h"
#import "FXBlurView.h"
//#import "PlayerViewController.h"
#import "playerTableViewController.h"
#import "search_labelController.h"
@interface Player_TagView ()
{
    float x;
    int lineCount;
    CGFloat lastPointY;
}
@end

@implementation Player_TagView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}
- (void)createBackgroundView
{
//    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
//    UIVisualEffectView *visual = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//    visual.frame = self.bounds;
//    [self addSubview:visual];

}
- (CGFloat)createTagView:(NSArray *)tags
{
    NSMutableArray *newTags = [NSMutableArray arrayWithArray:tags];
    if ([newTags containsObject:@"话题"] && [newTags containsObject:@"话#题"]) {
        [newTags removeObject:@"话#题"];
    } else if ([newTags containsObject:@"买卖"] && [newTags containsObject:@"买#卖"]) {
        [newTags removeObject:@"买#卖"];
    } else if ([newTags containsObject:@"读书"] && [newTags containsObject:@"读#书"]) {
        [newTags removeObject:@"读#书"];
    } else if ([newTags containsObject:@"音乐"] && [newTags containsObject:@"音#乐"]) {
        [newTags removeObject:@"音#乐"];
    } else if ([newTags containsObject:@"谈话"] && [newTags containsObject:@"谈#话"]) {
        [newTags removeObject:@"谈#话"];
    } else if ([newTags containsObject:@"点评"] && [newTags containsObject:@"点#评"]) {
        [newTags removeObject:@"点#评"];
    } else if ([newTags containsObject:@"记录"] && [newTags containsObject:@"记#录"]) {
        [newTags removeObject:@"记#录"];
    } else if ([newTags containsObject:@"观点"] && [newTags containsObject:@"观#点"]) {
        [newTags removeObject:@"观#点"];
    } else if ([newTags containsObject:@"段子"] && [newTags containsObject:@"段#子"]) {
        [newTags removeObject:@"段#子"];
    } else if ([newTags containsObject:@"其他"] && [newTags containsObject:@"其#他"]) {
        [newTags removeObject:@"其#他"];
    } else if ([newTags containsObject:@"故事"] && [newTags containsObject:@"故#事"]) {
        [newTags removeObject:@"故#事"];
    } else if ([newTags containsObject:@"教学"] && [newTags containsObject:@"教#学"]) {
        [newTags removeObject:@"教#学"];
    } else if ([newTags containsObject:@"旅行"] && [newTags containsObject:@"旅#行"]) {
        [newTags removeObject:@"旅#行"];
    } else if ([newTags containsObject:@"电影"] && [newTags containsObject:@"电#影"]) {
        [newTags removeObject:@"电#影"];
    } else if ([newTags containsObject:@"影评"] && [newTags containsObject:@"影#评"]) {
        [newTags removeObject:@"影#评"];
    }




    x = 10;
    lineCount = 1;
    lastPointY = 0.0;
    for (int i = 0; i < newTags.count; i++) {
        NSString *tag = newTags[i];
        self.tagLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_tagLabel setNumberOfLines:0];
//        _tagLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
//        _tagLabel.layer.borderWidth = 1.5;
        _tagLabel.textAlignment = NSTextAlignmentCenter;
        _tagLabel.textColor = [UIColor whiteColor];
       lastPointY = [self setLabelText:tag];
        _tagLabel.userInteractionEnabled = YES;
        _tagLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_tagLabel];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taglabelTap:)];
        [_tagLabel addGestureRecognizer:tap];
    }
    x = 10;
    lineCount = 1;
    return lastPointY;
}
- (CGFloat)setLabelText:(NSString *)text{
    NSString *str = [self checkTextIsEquelClassify:text];
    UIFont *font = [UIFont fontWithName:@"Arial" size:14];
    _tagLabel.font = font;
    _tagLabel.text = [NSString stringWithFormat:@"#%@",str];
    CGSize deSize = [_tagLabel sizeThatFits:CGSizeMake(300,30)];
    if ([UIScreen mainScreen].bounds.size.width - x - 30 < deSize.width ) {
        x = 10;
        lineCount++;
        if (lineCount == 2){
            _tagLabel.frame = CGRectMake(x, 35, deSize.width + 20, 25);
        } else if (lineCount == 3) {
            _tagLabel.frame = CGRectMake(x, 65, deSize.width + 20, 25);
        }
        x = x + deSize.width + 25;
    } else {
        if (lineCount == 1) {
            _tagLabel.frame = CGRectMake(x, 5, deSize.width + 20, 25);
            x = x + deSize.width + 25;
        } else {
            _tagLabel.frame = CGRectMake(x, 35, deSize.width + 20, 25);
            x = x + deSize.width + 25;
        }
        
    }
    return (_tagLabel.frame.origin.y + _tagLabel.frame.size.height);
}
- (NSString *)checkTextIsEquelClassify:(NSString *)text
{
    if ([text isEqualToString:@"记#录"]) {
        return @"记录";
    } else if ([text isEqualToString:@"点#评"]) {
        return @"点评";
    } else if ([text isEqualToString:@"谈#话"]) {
        return @"谈话";
    } else if ([text isEqualToString:@"音#乐"]) {
        return @"音乐";
    } else if ([text isEqualToString:@"买#卖"]) {
        return @"买卖";
    } else if ([text isEqualToString:@"读#书"]) {
        return @"读书";
    } else if ([text isEqualToString:@"话#题"]) {
        return @"话题";
    } else if ([text isEqualToString:@"观#点"]) {
        return @"观点";
    } else if ([text isEqualToString:@"段#子"]) {
        return @"段子";
    } else if ([text isEqualToString:@"其#他"]) {
        return @"其他";
    } else if ([text isEqualToString:@"故#事"]) {
        return @"故事";
    } else if ([text isEqualToString:@"教#学"]) {
        return @"教学";
    } else if ([text isEqualToString:@"旅#行"]) {
        return @"旅行";
    } else if ([text isEqualToString:@"电#影"]) {
        return @"电影";
    } else if ([text isEqualToString:@"影#评"]) {
        return @"电影";
    } else {
        return text;
    }
}
- (void)taglabelTap:(UITapGestureRecognizer *)reg
{
    UILabel *label = (UILabel *)reg.view;
    playerTableViewController * player =(playerTableViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
    search_labelController *searchLabelVC = [[search_labelController alloc]init];
    [searchLabelVC getTitle:label.text];
    [player.navigationController pushViewController:searchLabelVC animated:YES];
}
//- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font
//{
//    CGRect rect = [string boundingRectWithSize:CGSizeMake(40, 40)//限制最大的宽度和高度
//                                       options: NSStringDrawingUsesFontLeading//采用换行模式
//                                    attributes:@{NSFontAttributeName: font}//传人的字体字典
//                                       context:nil];
//    
//    return rect.size;
//}

@end
